package com.selfdriving.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Spinner;

import com.selfdriving.BaseApplication;
import com.selfdriving.R;
import com.selfdriving.bean.Preference;
import com.selfdriving.view.HomeButton;
import com.selfdriving.view.HomeButton.HomeClickListener;

public class WAAmbitusServiceList
{
	View view;
	HomeButton homeButton1;
	HomeButton homeButton2;
	HomeButton homeButton3;
	HomeButton homeButton4;
	HomeButton homeButton5;
	HomeButton homeButton6;
	HomeButton homeButton7;
	HomeButton homeButton8;
	HomeButton homeButton9;
	private PopupWindow popup;
	private View MenuView;
	private LayoutInflater inflater;
	private Spinner spinner;
	private Button but1;
	private Button but2;
	private Button but3;
	BaseApplication application;
	Context context;

	public WAAmbitusServiceList(BaseApplication application, Context context)
	{
		this.application = application;
		this.context = context;
		inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.w_ambitus_service, null);
		initViews();
		initEvent();
	}

	public void initViews()
	{

		MenuView = inflater.inflate(R.layout.w_ambitus_dialog, null);
		homeButton1 = (HomeButton) view.findViewById(R.id.ambitus_service_but1);
		homeButton2 = (HomeButton) view.findViewById(R.id.ambitus_service_but2);
		homeButton3 = (HomeButton) view.findViewById(R.id.ambitus_service_but3);
		homeButton4 = (HomeButton) view.findViewById(R.id.ambitus_service_but4);
		homeButton5 = (HomeButton) view.findViewById(R.id.ambitus_service_but5);
		homeButton6 = (HomeButton) view.findViewById(R.id.ambitus_service_but6);
		homeButton7 = (HomeButton) view.findViewById(R.id.ambitus_service_but7);
		homeButton8 = (HomeButton) view.findViewById(R.id.ambitus_service_but8);
		homeButton9 = (HomeButton) view.findViewById(R.id.ambitus_service_but9);
		spinner = (Spinner) MenuView.findViewById(R.id.ambitus_dialog_spinner);
		but1 = (Button) MenuView.findViewById(R.id.ambitus_dialog_but1);
		but2 = (Button) MenuView.findViewById(R.id.ambitus_dialog_but2);
		but3 = (Button) MenuView.findViewById(R.id.ambitus_dialog_but3);
		String[] mItems = context.getResources().getStringArray(
				R.array.condition);
		ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
				context, android.R.layout.simple_spinner_item, mItems);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
	}

	public void initEvent()
	{
		homeButton1.setOnHomeClick(new HomeClickListener()
		{
			@Override
			public void onclick()
			{
				startActivityToAmbitusQuery("餐饮");
			}
		});
		homeButton2.setOnHomeClick(new HomeClickListener()
		{
			@Override
			public void onclick()
			{
				startActivityToAmbitusQuery("酒店");
			}
		});
		homeButton3.setOnHomeClick(new HomeClickListener()
		{
			@Override
			public void onclick()
			{
				startActivityToAmbitusQuery("景区");
			}
		});
		homeButton4.setOnHomeClick(new HomeClickListener()
		{
			@Override
			public void onclick()
			{
				startActivityToAmbitusQuery("酒吧");
			}
		});
		homeButton5.setOnHomeClick(new HomeClickListener()
		{
			@Override
			public void onclick()
			{
				startActivityToAmbitusQuery("沐浴桑拿");
			}
		});
		homeButton6.setOnHomeClick(homeClickListener);
		homeButton7.setOnHomeClick(homeClickListener);
		homeButton8.setOnHomeClick(homeClickListener);
		homeButton9.setOnHomeClick(homeClickListener);
		but1.setOnClickListener(clickListener);
		but2.setOnClickListener(clickListener);
		but3.setOnClickListener(clickListener);
	}

	HomeClickListener homeClickListener = new HomeClickListener()
	{
		@Override
		public void onclick()
		{
			application.showShortToast(context, "稍候开通此功能");
		}
	};

	public void startActivityToAmbitusQuery(String keyWord)
	{
		boolean isLogin = BaseActivity.getBoolean(Preference.IS_LOGIN, false);
		if (!isLogin)
		{
			Intent intent = new Intent(context, RegisterActivity.class);
			context.startActivity(intent);
		} else
		{
			Intent intent = new Intent(context, WActivityAmbitusQuery.class);
			intent.putExtra("keyWord", keyWord);
			context.startActivity(intent);
		}

	}

	OnClickListener clickListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			switch (v.getId())
			{
			case R.id.ambitus_dialog_but1:
				if (but1.isSelected())
				{
					but1.setSelected(false);
					but1.setTextColor(Color.BLACK);
				} else
				{
					but1.setTextColor(Color.WHITE);
					but1.setSelected(true);
				}
				break;

			case R.id.ambitus_dialog_but2:
				if (but2.isSelected())
				{
					but2.setTextColor(Color.BLACK);
					but2.setSelected(false);
				} else
				{
					but2.setTextColor(Color.WHITE);
					but2.setSelected(true);
				}
				break;
			case R.id.ambitus_dialog_but3:
				boolean isLogin = BaseActivity.getBoolean(Preference.IS_LOGIN,
						false);
				if (!isLogin)
				{
					Intent intent = new Intent(context, RegisterActivity.class);
					context.startActivity(intent);
				} else
				{
					// Intent intent = new Intent(context,
					// WActivityAmbitusQuery.class);
					// intent.putExtra("keyWord", keyWord);
					// context.startActivity(intent);
				}
				break;
			}
		}
	};

	private void showWindow(View layoutList)
	{
		if (popup == null)
		{
			// 创建一个PopuWidow对象
			popup = new PopupWindow(MenuView,
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
		}
		// 使其聚集
		popup.setFocusable(true);
		// 设置允许在外点击消失
		popup.setOutsideTouchable(true);
		// 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
		popup.setBackgroundDrawable(new BitmapDrawable());
		popup.setTouchInterceptor(new OnTouchListener()
		{
			public boolean onTouch(View view, MotionEvent event)
			{
				if (event.getAction() == MotionEvent.ACTION_OUTSIDE)
				{
					popup.dismiss();
					return true;
				}
				return false;
			}
		});
		popup.showAsDropDown(layoutList);
		popup.showAtLocation(layoutList, Gravity.CENTER_HORIZONTAL, 0, 0);
	}

	public View getView()
	{
		return view;
	}
}
